/* Copyright (C) 2005-2013, Unigine Corp. All rights reserved.
 *
 * File:    fragment_base_sample.h
 * Desc:    Mesh base sample shader
 * Version: 1.02
 * Author:  Alexander Zapryagaev <frustum@unigine.com>
 *
 * This file is part of the Unigine engine (http://unigine.com/).
 *
 * Your use and or redistribution of this software in source and / or
 * binary form, with or without modification, is subject to: (i) your
 * ongoing acceptance of and compliance with the terms and conditions of
 * the Unigine License Agreement; and (ii) your inclusion of this notice
 * in any version of this software that you use or redistribute.
 * A copy of the Unigine License Agreement is available by contacting
 * Unigine Corp. at http://unigine.com/
 */

/******************************************************************************\
*
* OpenGL/OpenGLES
*
\******************************************************************************/

#ifdef OPENGL || OPENGLES
	
	fixed4 diffuse = texture2DAlpha(s_texture_0,s_texcoord_0.xy);
	fixed3 normal = texture2DNormal(s_texture_1,s_texcoord_0.xy);
	fixed4 specular = texture2D(s_texture_2,s_texcoord_0.xy);
	
	#ifdef OVERLAY_0 || OVERLAY_1 || MULTIPLY_0 || MULTIPLY_1
		
		#ifdef OVERLAY_0 || MULTIPLY_0
			half2 detail_texcoord = s_texcoord_0.xy * detail_transform.xy + detail_transform.zw;
		#elif OVERLAY_1 || MULTIPLY_1
			half2 detail_texcoord = s_texcoord_0.zw * detail_transform.xy + detail_transform.zw;
		#endif
		
		fixed4 detail_diffuse = texture2D(s_texture_3,detail_texcoord);
		
		#ifdef ALPHA_TEST
			fixed blend = s_material_detail.x * detail_diffuse.w;
		#else
			fixed blend = s_material_detail.x * diffuse.w * detail_diffuse.w;
		#endif
		
		#ifdef OVERLAY_0 || OVERLAY_1
			diffuse.xyz = saturate(diffuse.xyz + (detail_diffuse.xyz * 2.0f - 1.0f) * blend);
		#else
			diffuse.xyz = diffuse.xyz * saturate(detail_diffuse.xyz + 1.0f - blend);
		#endif
		
	#endif
	
	#ifdef USE_SRGB
		diffuse.xyz *= diffuse.xyz;
		specular.xyz *= specular.xyz;
	#endif
	
/******************************************************************************\
*
* Direct3D11/Direct3D119
*
\******************************************************************************/

#elif DIRECT3D11 || DIRECT3D119
	
	half4 diffuse = texture2DAlpha(s_texture_0,s_sampler_0,IN.texcoord_0.xy);
	half3 normal = texture2DNormal(s_texture_1,s_sampler_1,IN.texcoord_0.xy);
	half4 specular = s_texture_2.Sample(s_sampler_2,IN.texcoord_0.xy);
	
	#ifdef OVERLAY_0 || OVERLAY_1 || MULTIPLY_0 || MULTIPLY_1
		
		#ifdef OVERLAY_0 || MULTIPLY_0
			half2 detail_texcoord = IN.texcoord_0.xy * detail_transform.xy + detail_transform.zw;
		#else
			half2 detail_texcoord = IN.texcoord_0.zw * detail_transform.xy + detail_transform.zw;
		#endif
		
		half4 detail_diffuse = s_texture_3.Sample(s_sampler_3,detail_texcoord);
		
		#ifdef ALPHA_TEST
			half blend = s_material_detail.x * detail_diffuse.w;
		#else
			half blend = s_material_detail.x * (diffuse.w * detail_diffuse.w);
		#endif
		
		#ifdef OVERLAY_0 || OVERLAY_1
			diffuse.xyz = saturate(diffuse.xyz + (detail_diffuse.xyz * 2.0f - 1.0f) * blend);
		#else
			diffuse.xyz = diffuse.xyz * saturate(detail_diffuse.xyz + 1.0f - blend);
		#endif
		
	#endif
	
	#ifdef USE_SRGB
		diffuse.xyz *= diffuse.xyz;
		specular.xyz *= specular.xyz;
	#endif
	
#endif
